/*
 * ObjectConverter.java
 *
 * Created on May 17, 2007, 5:40 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.registration.web.jsf;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import static org.apache.commons.lang.StringUtils.isBlank;
/**
 *
 * @author lruiz
 */
public class NumberConverter extends javax.faces.convert.NumberConverter{
    public NumberConverter(){
        setPattern("#,##0.00");
    }
    
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
        if(isBlank(value)){
            value = "0";
        }
        return super.getAsObject(facesContext, uiComponent, value);
    }
    
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object object) {
        if(object != null && ((Number)object).doubleValue() == 0){
            return "";
        }
        return super.getAsString(facesContext, uiComponent, object);
    }
}
